package com.atguigu.testsso.client1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class loginController {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @GetMapping("/person")
    public String person(HttpSession session, @RequestParam(value = "sso_token",required = false)String token){
        if (!StringUtils.isEmpty(token)){
            //不为空，是登录服务器返回的请求
            String s = stringRedisTemplate.opsForValue().get(token);
            session.setAttribute("username",s);
            return "person";
        }
        Object username = session.getAttribute("username");
        if (username==null){

            System.out.println("session为空");
            return "redirect:http://sso.com:8500/sso?url=http://client1.com:8501/person";
        }

        return "person";
    }
}
